package com.ui;

import com.service.ClassService;
import com.model.Class;
import com.database.DatabaseConnection;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;
import java.util.function.Consumer;

public class ClassManagementUI extends JFrame {
    private ClassService classService;
    private JTable table;
    private DefaultTableModel tableModel;
    private JTextField searchField;
    private Consumer<String> onClassSelected; // 确保这是 Consumer<String> 类型

    public ClassManagementUI(ClassService classService, Consumer<String> onClassSelected) {
        this.classService = classService;
        this.onClassSelected = onClassSelected;
        setTitle("班级管理");
        setSize(600, 400);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel(new BorderLayout());
        searchField = new JTextField(20);
        JButton searchButton = new JButton("查询");
        JButton selectButton = new JButton("选择班级");
        JButton exportButton = new JButton("导出成绩");
        JButton deleteButton = new JButton("删除");
        JButton backButton = new JButton("返回");

        tableModel = new DefaultTableModel(new Object[]{"ID", "班级名称"}, 0);
        table = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(table);

        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
        topPanel.add(searchField);
        topPanel.add(searchButton);
        topPanel.add(selectButton);
        topPanel.add(exportButton);
        topPanel.add(deleteButton);
        topPanel.add(backButton);

        panel.add(topPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        add(panel);

        searchButton.addActionListener(e -> searchClasses());
        selectButton.addActionListener(e -> selectClass());
        exportButton.addActionListener(e -> exportGrades());
        deleteButton.addActionListener(e -> deleteClass());
        backButton.addActionListener(e -> goBack());

        setVisible(true);
    }

    private void searchClasses() {
        String searchText = searchField.getText().trim();
        tableModel.setRowCount(0); // 清空表格
        List<Class> classes = classService.getClassListByName(searchText.isEmpty() ? "%" : "%" + searchText + "%");
        for (Class cls : classes) {
            tableModel.addRow(new Object[]{cls.getId(), cls.getName()});
        }
    }

    private void selectClass() {
        int selectedRow = table.getSelectedRow();
        if (selectedRow != -1) {
            String className = (String) tableModel.getValueAt(selectedRow, 1);
            onClassSelected.accept(className); // 调用回调函数
            dispose(); // 关闭当前窗口
        } else {
            JOptionPane.showMessageDialog(this, "请先选择一个班级", "警告", JOptionPane.WARNING_MESSAGE);
        }
    }

    private void exportGrades() {
        // 实现成绩导出逻辑
    }

    private void deleteClass() {
        int selectedRow = table.getSelectedRow();
        if (selectedRow != -1) {
            int classId = (int) tableModel.getValueAt(selectedRow, 0);
            classService.deleteClass(classId);
            tableModel.removeRow(selectedRow);
            JOptionPane.showMessageDialog(this, "班级删除成功");
        } else {
            JOptionPane.showMessageDialog(this, "请先选择一个班级", "警告", JOptionPane.WARNING_MESSAGE);
        }
    }

    private void goBack() {
        dispose(); // 关闭当前窗口
    }
}